package com.example.renshiguanli.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.renshiguanli.config.PaginationConstant;
import com.example.renshiguanli.mapper.ContractMapper;
import com.example.renshiguanli.mapper.EmpMapper;
import com.example.renshiguanli.pojo.Contract;
import com.example.renshiguanli.pojo.Emp;
import com.example.renshiguanli.service.ContractService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ContractController {
    @Autowired
    private ContractService contractService;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private EmpMapper empMapper;
//    查询所有
    @GetMapping("/contracts")
    public String findAll(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum= PaginationConstant.CURRENT_NUM;
        }
        PageHelper.startPage(pageNum,PaginationConstant.PAGE_SIZE);
        List<Contract> allContract = contractService.findAllContract();
        model.addAttribute("contract",allContract);
        PageInfo<Contract> pageInfo=new PageInfo<>(allContract);
        model.addAttribute("pageInfo",pageInfo);
        return "contract/contract";
    }
//    添加信息
//    展示添加页面
    @GetMapping("/contract")
    public String findContract(Model model){
        List<Emp> allEmps = empMapper.getAllEmps();
        model.addAttribute("emps",allEmps);
        return "contract/add";
    }
//    添加
    @PostMapping("/contract")
    public String add(Contract contract){
        contractMapper.addByContract(contract);
        return "redirect:/contracts";
    }
//    编辑
//    根据id查询信息
    @GetMapping("/contract/{id}")
    public String findById(@PathVariable("id") Integer id,Model model){
        Contract contract = contractMapper.findId(id);
        model.addAttribute("contract",contract);
        Emp emp = empMapper.findById(id);
        model.addAttribute("emps",emp);
        return "contract/add";
    }
//    修改信息
    @PutMapping("/contract")
    public String update(Contract contract){
        contractMapper.updateByContract(contract);
        return "redirect:/contracts";
    }
//    删除信息
    @DeleteMapping("/contract/{id}")
    public String delete(@PathVariable Integer id){
        contractMapper.deleteContract(id);
        return "redirect:/contracts";
    }
//    查找信息
    @PostMapping("/consearch")
    public String consearch(String name,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,Model model){
        PageHelper.startPage(pageNum,PaginationConstant.PAGE_SIZE);
        List<Contract> contracts = contractService.query(name);
        model.addAttribute("contract",contracts);
        PageInfo<Contract> pageInfo=new PageInfo<>(contracts);
        model.addAttribute("pageInfo",pageInfo);
        return "contract/contract";
    }
}
